var express = require('express');
var router = express.Router();
var https = require('https');
var zlib = require('zlib');
var querystring = require('querystring');
router.get('/', function(request, response) {
    var contents = querystring.stringify({
        pageNum: '1',
        moxi_id: '12024',
        pro_id: '4937',
    });

    var options = {
        host: 'wds.modian.com',
        path: '/ajax_comment',
        method: 'POST',
        headers: {
            'Accept-Encoding': 'gzip, deflate, br',
            'Content-Length': contents.length,
            'Content-Type': 'application/x-www-form-urlencoded',
        }
    }

    var req = https.request(options, function(res) {

        console.log(res.statusCode);
        var json = [];
        res.on('data', function(chunk) {
            json.push(chunk);
        }).on('end', function() {
            var arr2 = [];
            json = Buffer.concat(json);
            zlib.gunzip(json, (err, data) => {

                var jsonData = JSON.parse(data.toString());
                var arr = jsonData.des;

                arr.forEach(function(el, index) {
                    var backData = {};
                    backData.ctime = el.ctime;
                    backData.content = el.content;
                    backData.fav = el.c_userinfo.icon;
                    backData.nickname = el.c_userinfo.nickname;
                    arr2.push(backData)
                });
                response.send(arr2)
            })
        });
    }).on('error', function(e) {
        console.log(e.message)
    });
    req.write(contents);
    req.end();
})
module.exports = router;